package com.dayaokeji.rhythmschoolstudent.client.mine.account;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ActivityStackManager;
import com.dayaokeji.rhythmschoolstudent.client.common.base.common.BaseActivity;
import com.dayaokeji.rhythmschoolstudent.utils.InputUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ObjectUtils;
import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.rhythmschoolstudent.utils.ToastUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.UserApi;
import com.dayaokeji.server_api.domain.ForgetPassword;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * 重置密码
 */
public class ForgetPasswordActivity extends BaseActivity {

    public static final String EXTRA_MOBILE_NUMBER = "mobile_number";

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_new_password)
    EditText etNewPassword;
    @BindView(R.id.et_confirm_new_password)
    EditText etConfirmNewPassword;

    private static final UserApi userApi = ApiUtils.getApi(UserApi.class);
    private String mobile;
    private Call<ServerResponse<Void>> forgetPasswordCall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        setSupportActionBar(toolbar);
        init();
    }

    private void init() {
        mobile = getIntent().getStringExtra(EXTRA_MOBILE_NUMBER);
        if (TextUtils.isEmpty(mobile)) {
            ToastUtils.warning(R.string.data_incorrect);
            return;
        }
    }

    @OnClick(R.id.btn_submit)
    public void onClicked(View view) {
        validate();
    }

    private void validate() {
        if (InputUtils.validateInput(etNewPassword,"请输入新密码") && InputUtils.validateInput(etConfirmNewPassword,"请再次输入新密码")) {
            String newPassword = etNewPassword.getText().toString().trim();
            String newConfirmPassword = etConfirmNewPassword.getText().toString().trim();
            if (ObjectUtils.equals(newPassword,newConfirmPassword)) {
                forgetPassword(newPassword);
            }else{
                ToastUtils.info("两次输入的密码不一致");
            }
        }
    }

    private void forgetPassword(String password) {
        ForgetPassword forgetPassword = new ForgetPassword();
        forgetPassword.setPassword(password);
        forgetPassword.setPhone(mobile);
        forgetPasswordCall = userApi.forgetPassword(forgetPassword);
        forgetPasswordCall.enqueue(new ServerCallback<Void>(this,"正在处理...") {
            @Override
            public void onResponse(boolean success, ServerResponse<Void> response) {
                if (success) {
                    ToastUtils.success("设置密码成功");
                    ActivityStackManager.finish();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (forgetPasswordCall != null) {
            forgetPasswordCall.cancel();
        }
        super.onDestroy();
    }
}
